﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Vigenere_Cipher.Model
{
    /// <summary>
    /// 加密过程
    /// </summary>
    internal class Encryption
    {
        /// <summary>
        /// 明文
        /// </summary>
        public string PlainText { get; set; }
        /// <summary>
        /// 密钥
        /// </summary>
        public string Key { get; set; }
        /// <summary>
        /// 密文
        /// </summary>
        public string CipherText { get; set; }
        /// <summary>
        /// 加密过程
        /// </summary>
        public void encryption()
        {
            for(int i = 0,j=0; i < PlainText.Length; i++,j++)
            {
                //根据密文长度分组
                if(j%Key.Length==0)
                {
                    j=0;         
                }
                //将字母转化为数字 并按规则加密
                int num = (CharToNo(PlainText[i]) + CharToNo(Key[j])) % 26;
                //数字转化为字母
                CipherText += Convert.ToChar(num + 'a');
            }
        }
        /// <summary>
        /// 字母转化为对应的数字
        /// </summary>
        /// <param name="ch"></param>
        /// <returns></returns>
        private int CharToNo(char ch)
        {
            return ch - 'a';
        }

    }
}
